package borge.web.teamspirit.server;

import javax.servlet.http.HttpSession;

import borge.web.teamspirit.client.data.Person;

public class SessionHandler {

	private HttpSession iSession;
	
	public SessionHandler(HttpSession aSession){
	
		iSession = aSession;
		iSession.setMaxInactiveInterval(60 * 60 * 24);
		
	}
	
	public boolean isActive(){
		return iSession != null && false; // what to check here??
	}
	
	public boolean isValid(String aId){
		
		if(iSession.isNew())
			return false;
		
		System.out.println("gwt sid = " + aId);
		System.out.println("cur session id = " + iSession.getId());
		
		if(!iSession.getId().equals(aId))
			return false;
		
		return true;
	}
	
	public void invalidate(String aId){
		
		if(!iSession.getId().equals(aId))
			System.out.println("invalidate: Warning: GWT id does not match session id");
		
		System.out.println("session is new = " + iSession.isNew());
		
		if(iSession != null)
			iSession.invalidate();
		
		iSession = null;
		//System.out.println("session is new = " + iSession.isNew());
	}
	
	public Object getObject(String aKey){
		
		return iSession.getAttribute(aKey);
	}
	
	public void saveObject(String aKey, Object aObject){
		
		iSession.setAttribute(aKey, aObject);
		
	}
	
	public String getSessionId(){
		return iSession.getId();
	}
	
}
